package servlets;

import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import commands.AddSharedFolderCommand;

/**
 * SharedFolderServlet implementation class ServerServlet - for sharing a folder
 * urls: /SharedFolderServlet/*
 */

public class SharedFolderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	AddSharedFolderCommand addSharedFolderCommand;
       
    /**
     * constructor
     * @see HttpServlet#HttpServlet()
     */
    public SharedFolderServlet() {
        super();
        // TODO Auto-generated constructor stub
        
        addSharedFolderCommand = new AddSharedFolderCommand();
    }

	/**
	 * do nothing
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setStatus(HttpServletResponse.SC_OK);
                
	}

	/**
	 * url: /SharedFolderServlet/folderName - share this folder
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        //get the user and the password from the session
        String userID = (String) request.getSession().getAttribute("userID");
        String password = (String) request.getSession().getAttribute("password");
        
        //get the user that going to have this shared folder from the form
        String sharedUserID = request.getParameter("userSharedID").toString();
        
        //get the folder name from the url
        String folderName = request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/")+1);
        
        out.println("<html>");
        out.println("<head>");
        out.println("<title> clever dropbox =) </title>");
        out.println("</head>");
        out.println("<body bgcolor=#66CCFF>");
        
        //share the folder
        Integer result = addSharedFolderCommand.action(userID, folderName, sharedUserID);
        
        //if succeed
        if ((result!=null) && (!result.equals(0)))
        {
        	out.println("<br> <br> <h3 align='center'> you shared folder "+ folderName + " successfully with "+ sharedUserID + "</h3><BR>");
        }
        else
        //if failed
        {
        	out.println("<br> <br> <h3 align='center'> the folder sharing was failed </h3> <BR>");
        	
        }
        
        out.println("<a href='/UserDropboxServlet/folder/" + folderName +"'> <h3 align='center'> back </h3> </a>");
        
        
        out.println("</body>");
        out.println("</html>");
        
        out.close();

	}


}
